<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class user_model extends CI_model {

    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    function coba_login($username, $password) {
        $username = mysql_real_escape_string($username);
        //echo "username $username mencoba login dengan password $password \n";

        $password = trim(sha1($password));

        $sql = "select * from pengguna where pengguna_username='$username'";
        echo $sql . "\n";
        $query = $this->db->query($sql);
        $hasil = array();
        $hasil['hasil'] = false;
        $detil_pengguna = array();
        foreach ($query->result() as $key => $row) {
            //echo "$password\n$row->pengguna_password\n";
            $string_compare = strcmp(trim($row->pengguna_password), trim($password));
            //echo "string compare = $string_compare";
            if ($string_compare == 0) {
                //$cocok=true;
                $hasil['hasil'] = true;
                echo "password cocok\n";
                //$detil_pengguna=$row;
                foreach ($row as $key => $value) {
                    $detil_pengguna[$key] = $value;
                }
                $hasil['detil_pengguna'] = $detil_pengguna;
                break;
            }
        }
        return $hasil;
    }

    function update($username, $nama, $nip, $skpd) {
        $session = $this->session->userdata('login');
        $this->db->where(array('pengguna_username' => $session['pengguna_username']));
        $u = $this->db->get('pengguna');

        $this->db->where(array('pengguna_username' => $username));
        $c = $this->db->get('pengguna');
        $sudahAda = false;
        $calon='';
        foreach ($c->result() as $row) {
            $sudahAda = true;
            $calon=$row;
        }
        foreach ($u->result() as $user) {
            if($sudahAda)
                $sudahAda = $sudahAda & !($user->pengguna_id == $calon->pengguna_id);
            if ($sudahAda) {
                return array('result' => 4);
            }
            if ($session['pengguna_id'] == $user->pengguna_id || ($session['flag'] == 1 && $user->flag == 2)) {
                $res = $this->db->update('pengguna', array(
                    'pengguna_username' => $username,
                    'pengguna_nama_display' => $nama,
                    'pengguna_nip' => $nip,
                    'nama_skpd' => $skpd
                        ), array('pengguna_id' => $user->pengguna_id));
                if ($session['pengguna_id'] == $user->pengguna_id) {
                    $this->db->where(array('pengguna_id' => $user->pengguna_id));
                    $q = $this->db->get('pengguna');
                    $q = $q->result();
                    $q = $q[0];
                    $q = $this->objectToArray($q);
                    $this->session->set_userdata(array('login' => $q));
                }
                return array('result' => 1, 'update' => $res);
            } else {
                return array('result' => 2);
            }
        }
        return array('result' => 3);
    }

    function update2($username, $pass_lama, $pass_baru, $nama, $nip, $skpd) {

        $session = $this->session->userdata('login');
        $this->db->where(array('pengguna_username' => $session['pengguna_username']));
        $u = $this->db->get('pengguna');

        $this->db->where(array('pengguna_username' => $username));
        $c = $this->db->get('pengguna');
        $sudahAda = false;
        $calon='';
        foreach ($c->result() as $row) {
            $sudahAda = true;
            $calon=$row;
        }
        foreach ($u->result() as $user) {
            //var_dump($user);
            if($sudahAda)
                $sudahAda = $sudahAda & !($user->pengguna_id == 
                    $calon->pengguna_id);
            if ($sudahAda) {
                return array('result' => 4);
            }
            if (($session['pengguna_id'] == $user->pengguna_id && trim(sha1($pass_lama)) == trim($user->pengguna_password)) || ($session['flag'] == 1 && $user->flag == 2)) {
                $res = $this->db->update('pengguna', array(
                    'pengguna_username' => $username,
                    'pengguna_nama_display' => $nama,
                    'pengguna_nip' => $nip,
                    'nama_skpd' => $skpd,
                    'pengguna_password' => trim(sha1($pass_baru))
                        ), array('pengguna_id' => $user->pengguna_id));
                if ($session['pengguna_id'] == $user->pengguna_id) {
                    $this->db->where(array('pengguna_id' => $user->pengguna_id));
                    $q = $this->db->get('pengguna');
                    $q = $q->result();
                    $q = $q[0];
                    $q = $this->objectToArray($q);
                    $this->session->set_userdata(array('login' => $q));
                }
                return array('result' => 1, 'update' => $res);
            } else {
                return array('result' => 2);
            }
        }
        return array('result' => 3);
    }

    function objectToArray($d) {
        if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
            $d = get_object_vars($d);
        }
        if (is_array($d)) {
            /*
             * Return array converted to object
             * Using __FUNCTION__ (Magic constant)
             * for recursive call
             */
            $r = array();
            foreach ($d as $key => $val) {
                $r[$key] = $val;
            }
            return $r;
        } else {
// Return array
            return $d;
        }
    }

    function get_list_user() {
        $session = $this->session->userdata('login');
        $sql = "select * from pengguna where flag>" . $session['flag'];

        $q = $this->db->query($sql);
        return $q->result();
    }

    function tambah_pengguna($username, $pass, $nama, $nip, $skpd) {
        $this->db->where(array('pengguna_username' => $username));
        $c = $this->db->get('pengguna');
        $sudahAda = false;
        foreach ($c->result() as $row) {
            $sudahAda = true;
        }
        if ($sudahAda) {
            return false;
        }
        if (strlen(trim($username)) == 0)
            return false;
        $r = $this->db->insert('pengguna', array(
            'pengguna_username' => $username,
            'pengguna_password' => trim(sha1($pass)),
            'pengguna_nama_display' => $nama,
            'pengguna_nip' => $nip,
            'nama_skpd' => $skpd,
            'flag' => 2
        ));
        //echo "create sql";
        return $r;
    }

    function get_user_by_id($id_user) {
        $this->db->where(array('pengguna_id' => $id_user));
        $u = $this->db->get('pengguna');
        $r = null;
        foreach ($u->result() as $user) {
            $r = $user;
            break;
        }
        return $r;
    }

    function update3($id_user, $username, $pass, $nama, $nip, $skpd) {
        $session = $this->session->userdata('login');
        $this->db->where(array('pengguna_username' => $session['pengguna_username']));
        $u = $this->db->get('pengguna');

        $this->db->where(array('pengguna_username' => $username));
        $c = $this->db->get('pengguna');
        $sudahAda = false;
        $calon='';
        foreach ($c->result() as $row) {
            $sudahAda = true;
            $calon=$row;
        }
        foreach ($u as $user) {
            if($sudahAda)
                $sudahAda = $sudahAda & !($user->pengguna_id == $calon->pengguna_id);
            if ($sudahAda) {
                return array('result' => 4);
            }
        }
        $r = $this->db->update('pengguna', array(
            'pengguna_username' => $username,
            'pengguna_password' => trim(sha1($pass)),
            'pengguna_nama_display' => $nama,
            'pengguna_nip' => $nip,
            'nama_skpd' => $skpd
                ), array('pengguna_id' => $id_user));
    }

    function hapus_user($id) {
        $r = $this->db->delete('pengguna', array('pengguna_id' => $id));
        return $r;
    }

}
